﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Clueless
{
    public class CardDeck
    {
        private LinkedList<Card> cards;

        public CardDeck(bool isMainDeck = false)
        {
            cards = new LinkedList<Card>();

            if (isMainDeck)
            {
                GenerateCards();
            }


        }
    
        public LinkedList<Card> Cards
        {
            get
            {
                return cards;
            }
            set
            {
                cards = value;
            }
        }

        public void AddCard(Card c)
        {
            cards.AddLast(c);
        }

        public Card GetCard(String name)
        {
            foreach (Card c in cards)
            {
                if (c.Name == name)
                    return c;
            }

            throw new Exception("Failed to find a card exception.  CardDeck.GetCard.");
        }

        public void GenerateCharacters()
        {
            // Characters
            AddCard(new Card("Colonel Mustard", CardType.Character));
            AddCard(new Card("Professor Plum", CardType.Character));
            AddCard(new Card("Mrs. Peacock", CardType.Character));
            AddCard(new Card("Miss Scarlet", CardType.Character));
            AddCard(new Card("Mr. Green", CardType.Character));
            AddCard(new Card("Mrs. White", CardType.Character));
        }

        public void GenerateCards()
        {
            GenerateCharacters();

            // Rooms
            AddCard(new Card("Kitchen", CardType.Room));
            AddCard(new Card("Ballroom", CardType.Room));
            AddCard(new Card("Conservatory", CardType.Room));
            AddCard(new Card("Billard Room", CardType.Room));
            AddCard(new Card("Library", CardType.Room));
            AddCard(new Card("Study", CardType.Room));
            AddCard(new Card("Hall", CardType.Room));
            AddCard(new Card("Lounge", CardType.Room));
            AddCard(new Card("Dining Room", CardType.Room));

            // Weapons
            AddCard(new Card("Lead Pipe", CardType.Weapon));
            AddCard(new Card("Candlestick", CardType.Weapon));
            AddCard(new Card("Dagger", CardType.Weapon));
            AddCard(new Card("Revolver", CardType.Weapon));
            AddCard(new Card("Rope", CardType.Weapon));
            AddCard(new Card("Spanner", CardType.Weapon));
        }   

        public void Shuffle()
        {
            Random rand = new Random();

            // Shuffle the deck between 10 and 50 itterations
            for (int j = 0; j < rand.Next(10,50); j++)
            {
                // Use a pseudo random sort on the cards (only lets the cards move ~1 spot per itteration)
                for (LinkedListNode<Card> n = cards.First; n != null; n = n.Next)
                {
                    Card v = n.Value;
                    if (rand.Next(0, 2) == 1)
                    {
                        n.Value = cards.Last.Value;
                        cards.Last.Value = v;
                    }
                    else
                    {
                        n.Value = cards.First.Value;
                        cards.First.Value = v;
                    }
                }
            }
        }

        public void Remove(Card c)
        {
            Cards.Remove(c);
        }

        public void RemoveSolution(Card c1, Card c2, Card c3)
        {
            Cards.Remove(c1);
            Cards.Remove(c2);
            Cards.Remove(c3);
        }
    }
}
